package com.junxonline.instant.admin.controller.config;

import com.junxonline.instant.dao.entity.vo.common.IdsVO;
import com.junxonline.instant.dao.entity.vo.config.DataSourceVO;
import com.junxonline.instant.admin.service.config.DataSourceService;
import com.junxonline.instant.common.annotation.OperationLog;
import com.junxonline.instant.common.entity.common.CommonReqPage;
import com.junxonline.instant.common.entity.common.CommonResult;
import com.junxonline.instant.common.enumeration.AdminErrorEnum;
import com.junxonline.instant.common.enumeration.OperationEnum;
import com.junxonline.instant.common.util.admin.TokenUtils;
import lombok.extern.log4j.Log4j2;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 数据源管理Controller
 *
 * @author JunX
 * @date 2021-08-02
 */
@Log4j2
@RestController
@RequestMapping(value = "/config/dataSource")
public class DataSourceController {

    @Autowired
    private HttpServletRequest request;

    @Autowired
    private TokenUtils tokenUtils;

    @Autowired
    private DataSourceService dataSourceService;

    /**
     * 查询备份文件列表
     *
     * @param vo 请求参数
     * @return CommonResult
     */
    @RequiresPermissions(value = "config:dataSource:query")
    @OperationLog(label = "数据源管理-查询列表", type = OperationEnum.QUERY)
    @PostMapping(value = "/queryList")
    public CommonResult queryList(@RequestBody(required = false) CommonReqPage<DataSourceVO> vo) {
        return CommonResult.success(dataSourceService.queryList(vo));
    }

    /**
     * 查询备份表列表
     *
     * @param vo 请求参数
     * @return CommonResult
     */
    @RequiresPermissions(value = "config:dataSource:query")
    @OperationLog(label = "数据源管理-查询备份表列表", type = OperationEnum.QUERY)
    @PostMapping(value = "/queryTableTree")
    public CommonResult queryTableTree(@RequestBody(required = false) DataSourceVO vo) {
        if (vo == null || vo.getId() == null) {
            return CommonResult.fail(AdminErrorEnum.PARAMS_IS_NULL);
        }
        return CommonResult.success(dataSourceService.queryTableTree(vo.getId()));
    }

    /**
     * 查询数据库表列表
     *
     * @return CommonResult
     */
    @RequiresPermissions(value = "config:dataSource:query")
    @OperationLog(label = "数据源管理-查询数据库表列表", type = OperationEnum.QUERY)
    @PostMapping(value = "/queryTableTreeSelect")
    public CommonResult queryTableTreeSelect() {
        return CommonResult.success(dataSourceService.queryTableTreeSelect());
    }

    /**
     * 备份数据库
     *
     * @param idsVO 请求参数
     * @return CommonResult
     */
    @RequiresPermissions(value = "config:dataSource:backup")
    @OperationLog(label = "数据源管理-备份数据库", type = OperationEnum.EDIT)
    @PostMapping(value = "/backup")
    public CommonResult backup(@RequestBody(required = false) IdsVO<List<String>> idsVO) {
        if (idsVO == null || idsVO.getIds() == null || idsVO.getIds().size() <= 0) {
            return CommonResult.fail(AdminErrorEnum.PARAMS_IS_NULL);
        }
        String token = request.getHeader("Authorization");
        String username = tokenUtils.getUsername(token);
        dataSourceService.backup(username, idsVO.getIds());
        return CommonResult.success();
    }

    /**
     * 还原数据库
     *
     * @param vo 请求参数
     * @return CommonResult
     */
    @RequiresPermissions(value = "config:dataSource:restore")
    @OperationLog(label = "数据源管理-还原数据库", type = OperationEnum.EDIT)
    @PostMapping(value = "/restore")
    public CommonResult restore(@RequestBody(required = false) DataSourceVO vo) {
        if (vo == null || vo.getId() == null) {
            return CommonResult.fail(AdminErrorEnum.PARAMS_IS_NULL);
        }
        dataSourceService.restore(vo.getId());
        return CommonResult.success();
    }

    /**
     * 删除备份文件
     *
     * @param idsVO 请求参数
     * @return CommonResult
     */
    @RequiresPermissions(value = "config:dataSource:delete")
    @OperationLog(label = "数据源管理-删除备份文件", type = OperationEnum.DELETE)
    @PostMapping(value = "/delete")
    public CommonResult delete(@RequestBody(required = false) IdsVO<List<Long>> idsVO) {
        if (idsVO == null || idsVO.getIds() == null || idsVO.getIds().size() <= 0) {
            return CommonResult.fail(AdminErrorEnum.PARAMS_IS_NULL);
        }
        dataSourceService.delete(idsVO.getIds());
        return CommonResult.success();
    }

}
